package com.iw.adoms.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.iw.adoms.entity.Brand;
import com.iw.adoms.entity.CustomerInterview;
import com.iw.adoms.entity.CustomerInterviewDetail;
import com.iw.adoms.entity.editor.EntityPropertyEditor;
import com.iw.adoms.service.BrandService;
import com.iw.adoms.service.CustomerInterviewDetailService;
import com.iw.adoms.service.CustomerInterviewService;
import com.iw.core.orm.Filtration;
import com.iw.core.orm.Filtration.MatchType;
import com.iw.core.orm.PageData;
import com.iw.core.utils.WebUtils;

@Controller
@RequestMapping("/adoms/customerInterviewDetail")
public class CustomerInterviewDetailAction {
	@Resource
	private CustomerInterviewDetailService customerInterviewDetailService;
	@Resource
	private CustomerInterviewService customerInterviewService;
	@Resource
	private BrandService brandService;

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(CustomerInterview.class, new EntityPropertyEditor(CustomerInterview.class));
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), false));
    }	
	
	@RequestMapping("/list")
	public ModelMap list(@RequestParam(value="interviewId", required=false) Integer interviewId, HttpServletRequest request) {
		PageData<CustomerInterviewDetail> pageData = new PageData<CustomerInterviewDetail>();
		// 给pageData设置参数
		WebUtils.setPageDataParameter(request, pageData);
		if(interviewId != null) {
			pageData.getFiltrations().add(new Filtration(MatchType.EQ, interviewId, "interview.id"));
		}
		pageData = customerInterviewDetailService.find(pageData);

		return new ModelMap(pageData);
	}

	@RequestMapping(value = "/new")
	public String addForm(Model model) {
		CustomerInterviewDetail customerInterviewDetail = new CustomerInterviewDetail();
		model.addAttribute(customerInterviewDetail);
		
		List<Brand> brands = brandService.findAll();//TODO Filter by the authentication. 
		model.addAttribute("brands", brands);
		
		List<CustomerInterview> interviews = customerInterviewService.findAll();
		model.addAttribute("interviews", interviews);
		
		return "/adoms/customerInterviewDetail/input";
	}

	@RequestMapping(value = "/edit/{id}")
	public String editForm(@PathVariable("id") Integer id, Model model) {
		CustomerInterviewDetail customerInterviewDetail = customerInterviewDetailService.find(id);
		model.addAttribute(customerInterviewDetail);
		
		List<Brand> brands = brandService.findAll();//TODO Filter by the authentication. 
		model.addAttribute("brands", brands);		
		
		List<CustomerInterview> interviews = customerInterviewService.findAll();
		model.addAttribute("interviews", interviews);
		return "/adoms/customerInterviewDetail/input";
	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public String save(CustomerInterviewDetail customerInterviewDetail) {
		if (customerInterviewDetail.isNew()) {
			customerInterviewDetailService.save(customerInterviewDetail);
		} else {
			customerInterviewDetailService.update(customerInterviewDetail);
		}

		return "redirect:/adoms/customerInterviewDetail/list";// 重定向
	}

	@RequestMapping(value = "/delete/{id}")
	public String delete(@PathVariable("id") Integer id) {
		customerInterviewDetailService.delete(id);
		return "redirect:/adoms/customerInterviewDetail/list";// 重定向
	}

	@RequestMapping(value = "/visible/{id}")
	public String visible(@PathVariable("id") Integer id) {
		customerInterviewDetailService.visible(id);
		return "redirect:/adoms/customerInterviewDetail/list";// 重定向
	}

	@RequestMapping(value = "/unVisible/{id}")
	public String unVisible(@PathVariable("id") Integer id) {
		customerInterviewDetailService.unVisible(id);
		return "redirect:/adoms/customerInterviewDetail/list";// 重定向
	}

}
